﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Runtime.InteropServices;
using System.Web.Hosting;
using System.Web.UI.WebControls;
using System.Xml;

using DotNetNuke.Framework;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace DotNetNuke.Modules.UserDefinedTable
{



  public abstract class DataType
  {
    private static IDictionary<string, DataType> colDataTypes;
    private static object DataTypeLock = new object();


    protected enum DBTypes
    {
      Int32,
      Decimal,
      DateTime,
      String,
      Boolean
    }


    protected DataType()
    { }


    public static IDictionary<string, string> ContentDataTypes()
    {
      IDictionary<string, string> dictionary = new SortedDictionary<string, string>();
      foreach (DataType type in DataTypes.Values)
        if (type.IsUserDefinedField)
          dictionary.Add(type.GetLocalization(""), type.Name);
        
      return dictionary;
    }




    public virtual string GetLocalization(string Setting = "")
    {
      string name;

      if ( !string.IsNullOrEmpty(Setting) )
        name = this.Name + "_" + Setting;
      
      else
        name = this.Name;
      
      string str3 = DotNetNuke.Services.Localization.Localization.GetString(name, this.LocalRescourceFile);

      if (string.IsNullOrEmpty(str3))
      {
        DotNetNuke.Services.Exceptions.Exceptions.LogException(new MissingLocalizationForDataTypeException(string.Format("No Rescource found for DataType \"{0}\", Setting \"{1}\".", this.Name, Setting)));
        return name;
      }

      return str3;
    }




    public virtual void RenderValuesToHtmlInsideDataSet(DataSet ds, int ModuleId, bool noscript)
    { }




    public virtual void SetStylesAndFormats(BoundField Column, string Format)
    { }




    public static IDictionary<string, string> SystemDataTypes()
    {
      IDictionary<string, string> dictionary = new SortedDictionary<string, string>();

      foreach (DataType type in DataTypes.Values)
        if (!type.IsUserDefinedField)
          dictionary.Add(type.GetLocalization(""), type.Name);
        
      return dictionary;
    }




    public static ICollection<string> AllDataTypes
    { get { return DataTypes.Keys; } }





    public static DataType ByName(string DataTypeName)
    {

      if (DataTypes.ContainsKey(DataTypeName))
        return DataTypes[DataTypeName];
      
      DotNetNuke.Services.Exceptions.Exceptions.LogException(new UnknownDataTypeException(string.Format("DataType \"{0}\" is not installed.", DataTypeName)));
      return DataTypes[DataTypeNames.UDT_DataType_String];

    }





    private static IDictionary<string, DataType> DataTypes
    {
      get
      {

        if (colDataTypes == null)
        {
          object dataTypeLock = DataTypeLock;
          //ObjectFlowControl.CheckForSyncLockOnValueType(dataTypeLock);
          lock (dataTypeLock)
          {
            colDataTypes = new Dictionary<string, DataType>();
            XmlDocument document = new XmlDocument();
            document.Load(HostingEnvironment.MapPath(string.Format("~{0}/DataTypes.config", Definition.PathOfModule)));
            string attribute;

            foreach (XmlElement current in document.SelectNodes("dataTypes/dataType"))
            {
              if (current.HasAttribute("typeName"))
                attribute = current.GetAttribute("typeName");
              
              else
                attribute = string.Format("DotNetNuke.Modules.UserDefinedTable.DataType{0}", current.GetAttribute("name"));
              
              DataType type = (DataType)Reflection.CreateObject(attribute, attribute);
              colDataTypes.Add(type.Name, type);
            }
          }
        }

        return colDataTypes;

      }
    }





    public virtual DotNetNuke.Modules.UserDefinedTable.EditControl EditControl
    {
      get
      {
        string typeName = "DotNetNuke.Modules.UserDefinedTable.Edit" + this.Name;
        return (EditControl)Reflection.CreateObject(typeName, typeName);
      }
    }





    public virtual string InputSettingDefault
    { get { return string.Empty; } }





    public virtual bool InputSettingsIsValueList
    { get { return this.SupportsInputSettings; } }




    public virtual IDictionary InputSettingsList
    { get { return null; } }





    public virtual bool IsSeparator
    { get { return false; } }





    public virtual bool IsUserDefinedField
    { get { return true; } }




    protected static IDictionary ListOfCommonDataTypes
    {
      get
      {
        SortedDictionary<string, string> dictionary = new SortedDictionary<string, string>();
        foreach (string str in Enum.GetNames(typeof(DBTypes)))
          dictionary.Add(ByName(str).GetLocalization(""), str);

        return dictionary;
      }
    }




    public virtual string LocalRescourceFile
    {
      get
      {
        return string.Format("{0}{1}{2}/SharedResources.resx", HostingEnvironment.ApplicationVirtualPath, Definition.PathOfModule, DotNetNuke.Services.Localization.Localization.LocalResourceDirectory);
      }
    }





    public abstract string Name { get; }

    [Obsolete("Obsolte, please use SystemDatatypes instead")]
    public static IDictionary<string, string> ObligateDataTypes
    {
      get
      {
        return SystemDataTypes();
      }
    }





    public virtual string SearchColumnAppendix
    { get { return ""; } }





    public virtual string SupportedCasts
    { get { return string.Format("String|{0}", this.Name); } }





    public virtual bool SupportsDefaultValue
    { get { return false; } }





    public virtual bool SupportsEditing
    { get { return false; } }





    public virtual bool SupportsEditStyle
    { get { return false; } }





    public virtual bool SupportsHideOnEdit
    { get { return false; } }





    public virtual bool SupportsInputSettings
    { get { return false; } }





    public virtual bool SupportsNormalizeFlag
    { get { return false; } }





    public virtual bool SupportsOutputSettings
    { get { return false; } }




    public virtual bool SupportsSearch
    { get { return false; } }




    public virtual bool SupportsValidation
    { get { return false; } }





    public virtual string SystemTypeName
    { get { return "String"; } }




    [Obsolete("Obsolte, please use ContentDataTypes instead")]
    public static IDictionary<string, string> UserDefinedDataTypes
    { get { return ContentDataTypes(); } }


  }

}

